package com.cskaoyan.utils;

import com.mysql.cj.jdbc.Driver;
import org.junit.BeforeClass;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;


import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class JDBCUtils {
    static Connection connection;
    static Properties properties;
    static String url;
    static String username;
    static String password;


    public static void initProperties() throws IOException {
        properties = new Properties();
        properties.load(new FileInputStream("C:\\Users\\85759\\IdeaProjects\\master\\untitled\\java-61-Stage2\\01-ClassWork\\Day08-myBatis\\demo4-crud\\src\\main\\resources\\jdbc.properties"));
        url = properties.getProperty("url");
        username = properties.getProperty("username");
        password = properties.getProperty("password");
    }

    public static Connection getConnection() throws SQLException, IOException {
        initProperties();
        // 加载驱动
        DriverManager.registerDriver(new Driver());


        connection = DriverManager.getConnection(url,username,password);

        return connection;
    }

    public static void release() throws SQLException {
        if (connection != null){
            connection.commit();
            connection.close();
        }
        connection.close();
    }
}
